using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.EventSystems;

public class MouseManager : Singleton<MouseManager>
{
    public Texture2D doorway, point, target, attack, arrow;
    private Vector2 mouseOffset = new Vector2(16, 16);
    public event Action<Vector3> OnMouseClicked;
    public event Action<GameObject> OnEnemyClicked;
    /// <summary>
    /// 射线碰撞到的物体
    /// </summary>
    private RaycastHit hitInfo;

    protected override void Awake()
    {
        base.Awake();
        DontDestroyOnLoad(this);
    }

    // Update is called once per frame
    void Update()
    {
        SetCursor();
        if (InteractWithUI())
        {
            return;
        }
        MouseControll();
    }

    //  随时改变鼠标样式
    private void SetCursor()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (InteractWithUI())
        {
            // 与 UI 交互时显示点击
            Cursor.SetCursor(point, Vector2.one * 2, CursorMode.Auto);
            return;
        }
        if (Physics.Raycast(ray, out hitInfo))
        {
            // WORKFLOW: 鼠标移到某些 tag 的物体时显示的样式
            switch (hitInfo.collider.gameObject.tag)
            {
                case "Ground":
                    Cursor.SetCursor(target, mouseOffset, CursorMode.Auto);
                    break;
                case "Enemy":
                    Cursor.SetCursor(attack, mouseOffset, CursorMode.Auto);
                    break;
                case "Portal":
                    Cursor.SetCursor(doorway, mouseOffset, CursorMode.Auto);
                    break;
                case "Item":
                    Cursor.SetCursor(point, mouseOffset, CursorMode.Auto);
                    break;
                default:
                    Cursor.SetCursor(arrow, mouseOffset, CursorMode.Auto);
                    break;
            }
        }

    }
    private void MouseControll()
    {
        if (Input.GetMouseButtonDown(0) && hitInfo.collider != null)
        {
            // WORKFLOW: 鼠标能点击到某类 tag 物体
            if (hitInfo.collider.gameObject.CompareTag("Ground"))
            {
                OnMouseClicked?.Invoke(hitInfo.point);
            }
            else if (hitInfo.collider.gameObject.CompareTag("Enemy"))
            {
                OnEnemyClicked?.Invoke(hitInfo.collider.gameObject);
            }
            else if (hitInfo.collider.gameObject.CompareTag("Attackable"))
            {
                OnEnemyClicked?.Invoke(hitInfo.collider.gameObject);
            }
            else if (hitInfo.collider.gameObject.CompareTag("Portal"))
            {
                OnMouseClicked?.Invoke(hitInfo.point);
            }
            else if (hitInfo.collider.gameObject.CompareTag("Item"))
            {
                OnMouseClicked?.Invoke(hitInfo.point);
            }
        }
    }
    private bool InteractWithUI()
    {
        if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
        {
            return true;
        }
        return false;
    }
}
